﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sim
{
    public partial class BookManage : Form
    {
        public BookManage()
        {
            InitializeComponent();
        }
        private void QueryAll()
        {
            string sql = "select * from Articles";

            var dt = DbHelper.GetDbHelper(sql);

            dataGridView1.DataSource = dt;

            //设置第 1 列的列标题（Id）
            dataGridView1.Columns[0].HeaderText = "ID";
            //设置第 2 列的列标题（Title）
            dataGridView1.Columns[1].HeaderText = "文章标题";
            //设置第 3 列的列标题（Author）
            dataGridView1.Columns[2].HeaderText = "文章作者";
            //设置第 4 列的列标题（CreatedTime）
            dataGridView1.Columns[3].HeaderText = "创建时间";
            //设置数据表格为只读
            dataGridView1.ReadOnly = true;
            //不允许添加行
            dataGridView1.AllowUserToAddRows = false;
            //背景为白色
            dataGridView1.BackgroundColor = Color.White;
            //只允许选中单行
            dataGridView1.MultiSelect = false;
            //整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void StuInfoManage_Load(object sender, EventArgs e)
        {
            QueryAll();
            skinEngine1.SkinFile = "Skins\\office2007.ssk";
        }

        //查询按钮
        private void Querybutton1_Click(object sender, EventArgs e)
        {
            Querys querys = new Querys();
            querys.Show();
        }

        //新增按钮
        private void button2_Click(object sender, EventArgs e)
        {
            Increased increased = new Increased();

            var res = increased.ShowDialog();

            if (res.Equals(DialogResult.OK))
            {
                string tempsql = string.Format("select * from Articles");

                var dt = DbHelper.GetDbHelper(tempsql);

                dataGridView1.DataSource = dt;
            }
        }

        //刷新按钮
        private void button5_Click(object sender, EventArgs e)
        {

            string sql = "select * from Articles";

            var dt = DbHelper.GetDbHelper(sql);

            dataGridView1.DataSource = dt;
        }

        //修改按钮
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var rows = dataGridView1.SelectedRows[0];
                var Id = (int)rows.Cells[0].Value;
                var Title = (string)rows.Cells[1].Value;
                var Author = (string)rows.Cells[2].Value;
                Updates updates = new Updates(Id, Title, Author);
                var res = updates.ShowDialog();
                if (res.Equals(DialogResult.OK))
                {
                    string tempsql = string.Format("select * from Articles where id={0}", Id);

                    var dt = DbHelper.GetDbHelper(tempsql);

                    dataGridView1.DataSource = dt;
                }
            }
            else
            {
                MessageBox.Show("你未选中任何行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //删除按钮
        private void button4_Click(object sender, EventArgs e)
        {
            var deleteRow = dataGridView1.SelectedRows.Count;//选中行数

            var content = dataGridView1.CurrentRow.Cells[0].Value;//获取主键

            if (dataGridView1.SelectedRows.Count > 0)
            {

                var res = MessageBox.Show("是否删除？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (res.Equals(DialogResult.Yes))
                {
                    if (deleteRow > 0)
                    {
                        DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;

                        drv.Delete();

                        string sql = string.Format("DELETE FROM Articles WHERE id = '{0}'", content);

                        var dtDelete = DbHelper.AddOrInsertOrUpdate(sql);

                        if (dtDelete)
                        {
                            MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            string tempsql = "select * from Articles";

                            var dt = DbHelper.GetDbHelper(tempsql);

                            dataGridView1.DataSource = dt;
                        }
                        else
                        {
                            MessageBox.Show("删除失败！自己找原因！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("取消成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            else
            {
                MessageBox.Show("你没有选中任何行！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        //右上角红叉事件
        private void StuInfoManage_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("是否退出系统？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                Environment.Exit(0);
            }
            else e.Cancel = true;
        }
    }
}
